package com.maxus.mini.open.api.controller.user;

import java.text.MessageFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maux.mini.data.api.redis.RedisService;
import com.maux.mini.data.api.user.UserService;
import com.maxus.mini.common.Constants;
import com.maxus.mini.common.WechatConstants;
import com.maxus.mini.common.utils.BeanUtilsExtended;
import com.maxus.mini.common.utils.HttpRequest;
import com.maxus.mini.common.utils.JSONUtil;
import com.maxus.mini.common.vo.ResBody;
import com.maxus.mini.entity.user.User;
import com.maxus.mini.open.api.controller.BaseController;
import com.maxus.mini.open.api.vo.req.P100000Vo;
import com.maxus.mini.open.api.vo.req.P100003Vo;
import com.maxus.mini.open.api.vo.req.P800000Vo;
import com.maxus.mini.open.api.vo.req.P800001Vo;
import com.maxus.mini.open.api.vo.req.P800006Vo;
import com.maxus.mini.open.api.vo.res.PR100003Vo;
import com.maxus.mini.open.api.vo.res.PR800000Vo;
import com.maxus.mini.open.api.vo.res.PR800001Vo;
import com.maxus.mini.open.api.vo.res.PR800006Vo;
import com.maxus.mini.open.api.vo.res.WechatVo;

/**
 * 用户controller
 * 
 * @author wujinsong
 *
 */
@RestController()
@RequestMapping(value = "api/v1.0/user")
public class UserController extends BaseController {
	@Reference(version = "1.0.0")
	private UserService<User, User> userService;
	@Reference(version = "1.0.0")
	private RedisService redisService;
	
	/**
	 * 保存用户
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "save")
	@ResponseBody()
	public User save(P100000Vo req) {
		User user = new User();
		BeanUtilsExtended.copyProperties(user, req);

		return userService.save(user);
	}

	/**
	 * 获取用户数量
	 * 
	 * @return
	 */
	@RequestMapping(value = "count")
	@ResponseBody()
	public ResBody count(P800000Vo req) {
		PR800000Vo retVo = new PR800000Vo();

		long count = userService.count();
		retVo.setCount(count);

		return retVo;
	}

	/**
	 * code换取 session_key接口
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "jscode2session")
	@ResponseBody()
	public ResBody jscode2session(P800001Vo req) {
		PR800001Vo retVo = new PR800001Vo();

		String surl = MessageFormat.format(WechatConstants.jscode2session, WechatConstants.appid,
				WechatConstants.secret, req.getCode());
		String res = HttpRequest.get(surl);

		try {
			WechatVo wechatVo = Constants.GSON.fromJson(res, WechatVo.class);
			retVo.setStatus(wechatVo.getErrcode());
			retVo.setMemo(wechatVo.getErrmsg());
		} catch (Exception e) {
			retVo = Constants.GSON.fromJson(res, PR800001Vo.class);
		}

		return retVo;
	}

	/**
	 * 编辑用户
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "maxau")
	@ResponseBody()
	public ResBody maxau(P100003Vo req) {
		PR100003Vo retVo = new PR100003Vo();
		User user = userService.findByOpenid(req.getOpenid());
		if (user != null) {
			BeanUtilsExtended.copyProperties(user, req);
		}

		userService.save(user);

		return retVo;
	}

	/**
	 * 获取用户
	 * 
	 * @param users
	 * @return
	 */
	public Iterable<User> save(List<User> users) {
		return userService.save(users);
	}

	/**
	 * 根据openid查找
	 * 
	 * @param openid
	 * @return
	 */
	@RequestMapping(value = "maxlmu")
	@ResponseBody()
	public ResBody maxlmu(P800006Vo p800006Vo) {
		PR800006Vo retVo = new PR800006Vo();
		
		if(!StringUtils.isEmpty(redisService.lpop(p800006Vo.getOpenid()))) {
			User user = JSONUtil.toBean(redisService.lpop(p800006Vo.getOpenid()),User.class);
			retVo.setUser(user);
			
			return retVo;
		}
		
		User user = userService.findByOpenid(p800006Vo.getOpenid());
		retVo.setUser(user);
		
		return retVo;
	}
}
